package com.atguigu.yygh.user.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.user.Account;
import com.atguigu.yygh.user.service.AccountService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/api/user/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 获取图灵验证码
     * 有效时长十分钟
     *
     * @return
     */
    @GetMapping("/captcha")
    @ApiOperation("获取图灵验证码")
    public void getCaptcha(HttpServletResponse response, @RequestParam("captcha_key") String captchaKey) throws IOException, FontFormatException {
        try {
            accountService.createCaptcha(response, captchaKey);
        } catch (Exception e) {
            log.info(e.toString());
        }
    }

    @ApiOperation("注册")
    @PostMapping("/register/{captcha}/{captcha_key}")
    public Result register(@PathVariable("captcha_key") String captchaKey,
                           @PathVariable("captcha") String captcha,
                           @RequestBody Account account,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        return accountService.register(captcha, captchaKey, account);
    }

    @ApiOperation("登录")
    @PostMapping("/login/{captcha}/{captcha_key}")
    public Result login(@PathVariable("captcha_key") String captchaKey,
                        @PathVariable("captcha") String captcha,
                        @RequestBody Account account
    ) {
        return accountService.login(captcha, captchaKey, account);
    }
}
